<?php
class MembergroupController extends Controller
{
	/**
    * Set layout
   */
   public $layout = '//layouts/cms/main';
   
   /**
	 * action array filter
	 */
   public function filters()
	{
		return array(
         'accessControl', // perform access control for CRUD operations
      );
	}
   
   /**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
   public function accessRules(){
      $rights = Role::getRights($this->id);
      //CVarDumper::dump($rights,10,true);die();
      return $rights;
   }
   
   /**
	 * Check login all action
	 */
   public function beforeAction($action){
      if(is_null(Yii::app()->user->id))
         $this->redirect('/admin/default/login');
      else
         return true;
   }
   
   public function actionIndex()
	{
	   Yii::app()->session['session_user'] = md5(time());  
	   $model = new Membergroup();
      $object = $model->findAll();
      foreach($object as $item){
         $list[] = $item->attributes;
      }
		$this->render('index',array('model' => $model,
                                  'list' => $list,
                                  'session_user' => Yii::app()->session['session_user']
                                  ));
	}
   
   /**
    * Create group member
   */
   public function actionCreate(){
      $message = '';
      $action_user = Yii::app()->request->getParam('action_user',''); // Biến xác định user ấn apply hay save
      $model = new Membergroup();
      if(isset($_POST['Membergroup'])){
         $model->attributes = $_POST['Membergroup'];
         if($model->validate()){
            $message = 'Thêm mới thành công';
            $model->save();
            if($action_user == 'save'){
               $this->redirect('/index.php/admin/'.$this->id);
            }
         }
      }
      $this->layout = '//layouts/cms/create';
      $this->pageTitle = 'Tạo nhóm';
      $this->render('create',array('model' => $model,
                                    'message' => $message,
                                    'session_user' => Yii::app()->session['session_user']
                                    ));
   }
   
   
   /**
    * Edit group member
   */
   public function actionEdit(){
      $session_user = Yii::app()->request->getParam('session_user');
      $action_user = Yii::app()->request->getParam('action_user',''); // Biến xác định user ấn apply hay save
      $message = '';
      $model = new Membergroup();
      $user_id = Yii::app()->user->id;
      if(is_null($user_id))
         $this->redirect('/site/login');
      else{
         $id = Yii::app()->request->getParam('id',0);
         $model = Membergroup::model()->findByPk($id);
         if(isset($_POST['Membergroup'])){
            if($session_user == Yii::app()->session['session_user']){
               if($model->validate()){
                  $model->MembergroupName = $_POST['Membergroup']['MembergroupName'];
                  $message = "Cập nhật thành công";
                  $model->save();
                  if($action_user == 'save'){
                     $this->redirect('/index.php/admin/'.$this->id);
                  }
                  
               }
            }
         }
         //CVarDumper::dump($model->attributes,10,true);die();
      }
      
      $this->layout = '//layouts/forgen';
      $this->render('edit',array('model' =>$model,
                                 'message' => $message,
                                 'session_user' => Yii::app()->session['session_user'],
                                 ));
   }
   
   /**
    * Deactive record
   */
	public function actionDeactive(){
      $error = 1;
      $record_affected = 0;
      $list_id = Yii::app()->request->getParam('list_id',array());
      $session_user = Yii::app()->request->getParam('session_user','');
      if($session_user == Yii::app()->session['session_user'])
      {
         if(count($list_id) > 0){
            $sql = 'UPDATE membergroup SET IsActive = 0 WHERE MemberGroupID IN(-1';
            foreach ($list_id as $item){
               if(is_numeric($item))
                  $sql .= ',' . $item;
            }
            $sql .= ')';
            $db = Yii::app()->db;
            $cmd = $db->createCommand($sql);
            $record_affected = $cmd->execute();
            if($record_affected >= 0){
               $error = 0;
            }else{
               $error = 1;
            }
         }
         
      }
      echo json_encode(array('error' => $error,
                             'record_affected' => $record_affected)); 
	}
   
   /**
    * Active record
   */
	public function actionActive(){
      $error = 1;
      $record_affected = 0;
      $list_id = Yii::app()->request->getParam('list_id',array());
      $session_user = Yii::app()->request->getParam('session_user','');
      if($session_user == Yii::app()->session['session_user'])
      {
         if(count($list_id) > 0){
            $sql = 'UPDATE membergroup SET IsActive = 1 WHERE MemberGroupID IN(-1';
            foreach ($list_id as $item){
               if(is_numeric($item))
                  $sql .= ',' . $item;
            }
            $sql .= ')';
            $db = Yii::app()->db;
            $cmd = $db->createCommand($sql);
            $record_affected = $cmd->execute();
            if($record_affected >= 0){
               $error = 0;
            }else{
               $error = 1;
            }
         }
         
      }
      echo json_encode(array('error' => $error,
                             'record_affected' => $record_affected)); 
	}
   
   /**
    * Delete record
   */
	public function actionDelete(){
      $error = 1;
      $record_affected = 0;
      $list_id = Yii::app()->request->getParam('list_id',array());
      $session_user = Yii::app()->request->getParam('session_user','');
      if($session_user == Yii::app()->session['session_user'])
      {
         if(count($list_id) > 0){
            $sql = 'DELETE FROM membergroup WHERE MemberGroupID IN(-1';
            foreach ($list_id as $item){
               if(is_numeric($item))
                  $sql .= ',' . $item;
            }
            $sql .= ')';
            $db = Yii::app()->db;
            $cmd = $db->createCommand($sql);
            $record_affected = $cmd->execute();
            if($record_affected >= 0){
               $error = 0;
            }else{
               $error = 1;
            }
         }
         
      }
      echo json_encode(array('error' => $error,
                             'record_affected' => $record_affected)); 
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}